package com.qifenqian.turbot.module.ui

import android.content.Context
import android.content.Intent
import android.support.design.widget.BottomNavigationView
import android.widget.Toast
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.config.Constant
import com.qifenqian.turbot.module.adapter.MainPagerAdapter
import com.qifenqian.turbot.module.entity.response.VersionEntity
import com.qifenqian.turbot.module.ui.loginetc.VERSION_ENTITY
import com.qifenqian.turbot.module.ui.marketing.MarketingFragment
import com.qifenqian.turbot.module.ui.order.OrderFragment
import com.qifenqian.turbot.module.ui.restaurant.RestaurantFragment
import com.qifenqian.turbot.module.ui.statistic.StatisticsFragment
import com.qifenqian.turbot.util.ActivityCollector
import com.qifenqian.turbot.util.TurbotUtil
import com.tmall.ultraviewpager.UltraViewPagerAdapter
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity<Nothing, Nothing>() {

    companion object {
        fun startThis(context: Context, versionEntity: VersionEntity?) {
            val intent = Intent(context, MainActivity::class.java)
            intent.putExtra(VERSION_ENTITY, versionEntity)
            context.startActivity(intent)
        }
    }

    private val fragmentList = listOf(StatisticsFragment(), MarketingFragment(), OrderFragment(), RestaurantFragment())

    override fun bindLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun bindPresenter(): Nothing? {
        return null
    }

    private var mVersionEntity: VersionEntity? = null

    override fun initial() {
        ActivityCollector.addActivity(this, Constant.TAG_ACTIVITY_COLLECTOR_LOGOUT)
        nav_main.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
        vp_main.setScroll(false)
        vp_main.adapter = UltraViewPagerAdapter(MainPagerAdapter(supportFragmentManager, fragmentList))
        mVersionEntity = intent.getParcelableExtra(VERSION_ENTITY)

        if (mVersionEntity != null && !TurbotUtil.getCurrentVersion(this).isEmpty()) {
            if (mVersionEntity!!.version != TurbotUtil.getCurrentVersion(this)) {
                showUpdateDialog()
            }
        }

    }

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.nav_tab_data       -> {
                vp_main.currentItem = 0
                return@OnNavigationItemSelectedListener true
            }
            R.id.nav_tab_marketing  -> {
                vp_main.currentItem = 1
                return@OnNavigationItemSelectedListener true
            }
            R.id.nav_tab_order      -> {
                vp_main.currentItem = 2
                return@OnNavigationItemSelectedListener true
            }
            R.id.nav_tab_restaurant -> {
                vp_main.currentItem = 3
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    private fun showUpdateDialog() {
        //更新的dialog
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this, Constant.TAG_ACTIVITY_COLLECTOR_LOGOUT)
    }

    private var time: Long = 0
    override fun onBackPressed() {
        if (System.currentTimeMillis() - time < 2000) {
            super.onBackPressed()
        } else {
            time = System.currentTimeMillis()
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
        }
    }

}
